package com.example.app2024.recylerViewdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.app2024.R;

import java.util.ArrayList;
import java.util.List;

public class recylerdemoActivity extends AppCompatActivity
{
      private RecyclerView mRecyclerView;
      private MyRecyclerViewAdapter mAdapter;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_recylerdemo);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            mRecyclerView = findViewById(R.id.recycler_view);

            // 线性布局
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            ////        横向排列ItemView
            //        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            ////        数据反向展示
            //        linearLayoutManager.setReverseLayout(true);

            mRecyclerView.setLayoutManager(linearLayoutManager);
            mAdapter = new MyRecyclerViewAdapter(this, mRecyclerView);
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());

            //        itemView点击事件监听

            mAdapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener()
            {
                  @Override
                  public void onItemClick(int position)
                  {
                        Toast.makeText(recylerdemoActivity.this, "第" + position + "数据被点击", Toast.LENGTH_SHORT).show();
                  }
            });

      }

      public void adddata(View view)
      {
            List<String> data = new ArrayList<>();

            for (int i = 0; i < 20; i++)
            {
                  String s = "第" + i + "条数据";
                  data.add(s);
            }

            mAdapter.setDataSource(data);
      }

      /**
       * 切换布局
       *
       * @param v
       */
      public void onChangeLayoutClick(View v)
      {
            //        从线性布局 切换为 网格布局
            if (mRecyclerView.getLayoutManager().getClass() == LinearLayoutManager.class)
            {
                  //            网格布局
                  GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
                  mRecyclerView.setLayoutManager(gridLayoutManager);
            }
            else if (mRecyclerView.getLayoutManager().getClass() == GridLayoutManager.class)
            {
                  //            瀑布流布局
                  StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                  mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
            }
            else
            {
                  // 线性布局
                  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                  mRecyclerView.setLayoutManager(linearLayoutManager);
            }
      }

      /**
       * 插入一条数据
       *
       * @param v
       */
      public void onInsertDataClick(View v)
      {
            mAdapter.addData(1);
      }

      /**
       * 删除一条数据
       *
       * @param v
       */
      public void onRemoveDataClick(View v)
      {
            mAdapter.removeData(1);
      }
}